package dl.jf.web.action.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import dl.jf.common.constants.Constants;
import dl.jf.common.model.LabelValue;
import dl.jf.common.model.MUser;
import dl.jf.service.iservice.admin.IAdminMemberService;
import dl.jf.web.util.Toolkit;
import dl.jf.web.action.BaseAction;
import dl.jf.web.form.admin.AdminMemberForm;

public class AdminMemberAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2112225653071118323L;

	private static final String POP_MEMBER_CREATE = "popAdminMemberCreate";

	private static final String POP_MEMBER_EDIT = "popAdminMemberEdit";

	public AdminMemberAction() {
		super("AdminMemberAction");
	}

	private AdminMemberForm memberForm;

	private IAdminMemberService adminMemberService;

	public IAdminMemberService getAdminMemberService() {
		return adminMemberService;
	}

	public void setAdminMemberService(IAdminMemberService adminMemberService) {
		this.adminMemberService = adminMemberService;
	}

	public AdminMemberForm getMemberForm() {
		return memberForm;
	}

	public void setMemberForm(AdminMemberForm memberForm) {
		this.memberForm = memberForm;
	}

	/**
	 * pagination block
	 * 
	 * @return
	 * @throws Exception
	 */
	private Integer pageNo = 1;
	private Integer everyPage = 10;
	private Integer total = 0;

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getEveryPage() {
		return everyPage;
	}

	public void setEveryPage(Integer everyPage) {
		this.everyPage = everyPage;
	}

	public Integer getTotal() {
		return this.total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	/**
	 * validation fields
	 */
	private String newUserId;

	public String getNewUserId() {
		return newUserId;
	}

	public void setNewUserId(String newUserId) {
		this.newUserId = newUserId;
	}

	public Integer getNewRoleId() {
		return newRoleId;
	}

	public void setNewRoleId(Integer newRoleId) {
		this.newRoleId = newRoleId;
	}

	public String getNewNickname() {
		return newNickname;
	}

	public void setNewNickname(String newNickname) {
		this.newNickname = newNickname;
	}

	public String getNewEmailAddress() {
		return newEmailAddress;
	}

	public void setNewEmailAddress(String newEmailAddress) {
		this.newEmailAddress = newEmailAddress;
	}

	public Integer getNewDisabled() {
		return newDisabled;
	}

	public void setNewDisabled(Integer newDisabled) {
		this.newDisabled = newDisabled;
	}

	public String getNewinsertDate() {
		return newinsertDate;
	}

	public void setNewinsertDate(String newinsertDate) {
		this.newinsertDate = newinsertDate;
	}

	public String getNewPhotoId() {
		return newPhotoId;
	}

	public void setNewPhotoId(String newPhotoId) {
		this.newPhotoId = newPhotoId;
	}

	public String getEditUserId() {
		return editUserId;
	}

	public void setEditUserId(String editUserId) {
		this.editUserId = editUserId;
	}


	private Integer newRoleId;

	private String newNickname;

	private String newEmailAddress;

	private Integer newDisabled;

	private String newinsertDate;

	private String newPhotoId;

	private String editUserId;

	private List<LabelValue> roleList = new ArrayList<LabelValue>();
	
	private List<LabelValue> disabledList = new ArrayList<LabelValue>();

	public List<LabelValue> getDisabledList() {
		if (Toolkit.isEmpty(memberForm.getDisabledList()) || Toolkit.isEmpty(disabledList)) {
			pushList();
			setDisabledList(memberForm.getDisabledList());
		}
		return disabledList;
	}

	public void setDisabledList(List<LabelValue> disabledList) {
		this.disabledList = disabledList;
	}

	public List<LabelValue> getRoleList() {
		if (Toolkit.isEmpty(memberForm.getRoleList()) || Toolkit.isEmpty(roleList)) {
			pushList();
			setRoleList(memberForm.getRoleList());
		}
		return roleList;
	}

	public void setRoleList(List<LabelValue> roleList) {
		this.roleList = roleList;
	}

	public String initial() throws Exception {
		pushList();
		return Constants.ACTION_RESULT_SUCCESS;
	}

	@Override
	public String execute() throws Exception {
		if (memberForm == null) {
			setMemberForm(new AdminMemberForm());
		}
		return initial();
	}

	private void pushList() {
		List<LabelValue> roleLabelList = null;
		try {
			roleLabelList = adminMemberService.getRoleLabelListById();
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}
		if (Toolkit.isEmpty(roleLabelList)) {
			roleLabelList = new ArrayList<LabelValue>();
		}
		memberForm.setRoleList(roleLabelList);

		List<LabelValue> disabledList = new ArrayList<LabelValue>();
		disabledList.add(new LabelValue(getText("msg_admin_member_disabled_no"), "0"));
		disabledList.add(new LabelValue(getText("msg_admin_member_disabled_yes"), "1"));
		memberForm.setDisabledList(disabledList);

	}

	@SuppressWarnings("unchecked")
	public String doSearch() throws Exception {
		MUser user = new MUser();
		if (memberForm == null) {
			setMemberForm(new AdminMemberForm());
		} else {
			user.setUserId(memberForm.getSearchUserId());
			user.setRoleId(memberForm.getSearchRoleId());
			user.setEmailAddress(memberForm.getSearchEmail());
			user.setDisabled(memberForm.getSearchDisabled());
			user.setSearchInsertDateFrom(memberForm.getSearchInsertDateFrom());
			user.setSearchInsertDateTo(memberForm.getSearchInsertDateTo());
			user.setUserName(Toolkit.getStringToUTF8(memberForm.getSearchNickname()));
		}

		user.setStartNo(everyPage * (pageNo - 1));
		user.setEndNo(user.getStartNo() + everyPage);
		Map<String, Object> map = adminMemberService.getUserListByUser(user);
		List<MUser> userList = (ArrayList<MUser>) map.get("list");
		total = (Integer) map.get("count") % everyPage > 0 ? (Integer) map.get("count") / everyPage
				+ 1 : (Integer) map.get("count") / everyPage;
		memberForm.setSearchNickname(user.getUserName());
		memberForm.setUserList(userList);
		pushList();
		return Constants.ACTION_RESULT_SUCCESS;
	}

	public String popMemberCreate() throws Exception {
		if (memberForm == null) {
			setMemberForm(new AdminMemberForm());
		}
		pushList();
		return POP_MEMBER_CREATE;
	}

	public String popMemberEdit() throws Exception {

		if (memberForm == null) {
			setMemberForm(new AdminMemberForm());
		}
		String editUserId = request.getParameter("editUserId");
		if (Toolkit.isEmpty(editUserId)) {
			getContext().put("nopermit", "true");
			return POP_MEMBER_EDIT;
		}
		MUser user = adminMemberService.getUserListByUserId(editUserId);
		if (user == null) {
			getContext().put("nouser", "true");
			getContext().put("editid", editUserId);
			return POP_MEMBER_CREATE;
		}

		setEditUserId(editUserId);
		setNewUserId(user.getUserId());
		setNewDisabled(user.getDisabled());
		setNewEmailAddress(user.getEmailAddress());
		setNewNickname(user.getUserName());
		setNewRoleId(user.getRoleId());
		pushList();
		return POP_MEMBER_EDIT;
	}

	public String doDeleteMembers() throws Exception {
		if (Toolkit.isEmpty(memberForm.getEditUserArray())) {
			return doSearch();
		}
		if (Toolkit.isEmpty((String) session.get("userId"))) {
			getContext().put("nopermit", "true");
			return Constants.ACTION_RESULT_TO_LOGIN;
		}
		try {
			adminMemberService.deleteUserByUserIdArray(memberForm.getEditUserArray());
		} catch (Exception ex) {
			logger.error("delete user error", ex);
		}
		return doSearch();
	}
}
